<?php
namespace Common\Library;

/**
 * Description of www.7kshu.com
 * @author gangguo
 */
class 7kshu{

    protected $BookArticle;
    protected $detailUrl;
    protected $book_id;
    protected $listInfo;
    protected $id;

    public function __construct($id,$url = ''){
        header("Content-Type:text/html;charset=utf-8;");
        $this->id = $id;
        if(empty($url)){
            $this->update();
        }else{
            $this->detailUrl = $url;
            $this->addNewBook();
        }
        exit('over good');
    }

    //添加一本新书
    protected function addNewBook(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/7kshu/addbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div id="main">','<div class="option">');
        $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<ul id="chapterlist">','<div id="tishi">');

        unset($getStr,$pathFile);
        //提取书籍名称
        $bookName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<h1>','</h1>');
        //提取作者
        $author = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'作者：','&nbsp;&nbsp;&nbsp;&nbsp;类别');
        //p($author);p($bookName);p($bookInfo);exit;
        if(empty($bookName) || empty($author)){
            $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
        }

        //作者处理
        $author_id = D('Common/BookAuthor')->getAuthorId($author);
        $Book = new \Common\Model\BookModel();
        $unique = md5($bookName.$author_id);
        //书本重复检测
        $this->book_id = $Book->checkBookRepeart($unique);
        //p($this->book_id);exit;
        if(empty($this->book_id)){//新书入库
            //提取简介
            $intro = '';
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($getStr,'类别：','<br>');
            $cate = \Common\Library\COM\CollectDo::changeCateName($cate);
            if(empty($cate)){
                $this->writeLog('入库错误,没有分类:'.var_export($cate,1).'@@'.var_export($bookInfo,1),'ERR');
            }
            //提取封面图片
            $image = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<img src="','" title="');
            //p($image);exit;
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);

            //书籍状态
            $bookStatus ='';// \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<span class="','"></span></div>');
            if($bookStatus == 'a'){
                $status = 'end';
            }else{
                $status = 'ing';
            }
            //封面图片处理
            $imagePath = \Common\Library\COM\CollectDo::doImagePath();

            $dataBook = array(
                'book_name'=>$bookName,
                'author_id'=>$author_id,
                'status'=>$status,
                'cate_id'=>$cate_id,
                'introduction'=> '', //htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$imagePath,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            //p($dataBook);exit;
            $this->book_id = $Book->add($dataBook);
            \Common\Library\COM\CollectDo::downPage($image, '.'.$imagePath);
        }
        unset($bookInfo,$start,$end);
        //新书开始入库
        if(empty($this->book_id)){
            $this->writeLog('入库错误,书本未入库:'.  var_export($Book->getLastSql(),1),'ERR');
        }else{
            $this->writeLog('bookinstall.新书开始入库:'.var_export($bookName,1));
        }
        $this->articleCollection();
    }

    //章节列表名称入库
    protected function articleCollection(){
        //章节名称和篇名入库
        if(empty($this->listInfo) || empty($this->book_id)){
            $this->writeLog('章节目录为空！!@Value:'.$this->book_id.'@URL:'.$this->detailUrl,'ERR');
        }
        //p($this->listInfo);
        $count = mb_substr_count($this->listInfo,'<dt>');
        $count1 = mb_substr_count($this->listInfo,'<dd>');
        $count = $count + $count1;
        $this->BookArticle = new \Common\Model\BookArticleModel();
        //获取最新章节
        $new = $this->BookArticle->where("`book_id`='{$this->book_id}'")->count('article_id');
        if($new['article_num'] > 0 && $new['article_num'] < $count){
            $continuef = true;
        }else{
            $continuef = FALSE;
        }
        for($i=0;$i<$count;$i++){
            $pian = mb_strpos($this->listInfo,'<dt>');
            //跳过已有的章节
            if($continuef && $new >= $i){
                if($pian === 0){
                    $now =  \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'','</dt>');
                    $piece_name =  \Common\Library\COM\CollectDo::getSubstr($now,'<dt>');
                    $piece_num =  \Common\Library\COM\CollectDo::getPieceNum($piece_name);
                    $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'</dt>','',FALSE);
                }else{
                    $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'</dd>','',FALSE);
                }
                continue;
            }
            if($pian === 0){ //发现篇名
                $now =  \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'','</dt>');
                $piece_name =  \Common\Library\COM\CollectDo::getSubstr($now,'<dt>');
                $piece_num =  \Common\Library\COM\CollectDo::getPieceNum($piece_name);
                $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'</dt>','',FALSE);
                continue;
            }
            $now =  \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'','</dd>');
            $article_name = \Common\Library\COM\CollectDo::getSubstr($now,'html">','</a>');
            if(empty($article_name)){
                $this->writeLog('章节名称不能为空!:'.$now.'@URL:'.$this->listInfo,'ERR');
            }
            $article_num = \Common\Library\COM\CollectDo::getArticleNum($article_name);
            $unique = md5($this->book_id.$article_name);
            $data = array('book_id'=>$this->book_id,'article_num'=>$article_num,'article_name'=>$article_name);
            $result = $this->BookArticle->checkArtcileRepeat($unique,$data);
            if($result){//重复章节检测
                $this->writeLog($article_name.' ====> 重复章节');
            }else{
                $this->writeLog($article_name.' ====> 章节入库');
                //获取章节内容url地址
                $article_url = \Common\Library\COM\CollectDo::getSubstr($now,'href="','">');
                //获取章节内容并存储
                $contentPath = $this->contentCollection($this->url.$article_url);
                $articleDate = array(
                    'book_id'=> $this->book_id,
                    'article_name'=> $article_name,
                    'create_time'=>  time(),
                    'year'=>  date('Y'),
                    'month'=>  date('m'),
                    'day'=>  date('d'),
                    'article_num'=> $article_num,
                    'update_time' => time(),
                    'unique'=>$unique,
                    'piece_num'=> $piece_num,
                    'piece_name'=> $piece_name,
                    'content'=>$contentPath
                );
                //p($articleDate);//exit;
                $article_id = $this->BookArticle->add($articleDate);
                if(empty($article_id)){
                    $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1),'ERR');
                }
            }
            $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($this->listInfo,'</dd>','',FALSE);
        }
        unset($this->BookArticle);
        $this->writeLog($this->detailUrl.'  本书入库完成!');
    }

    //章节内容入库
    protected function contentCollection($contentUrl = ''){
        if(empty($contentUrl)){
            $this->writeLog('内容章节URL地址空:'.var_export($contentUrl,1),'ERR');
        }
        $savePath = './Runtime/Collect/7kshu/content.html';
        \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
        $contentA = \Common\Library\COM\CollectDo::getHtml($savePath,'','utf-8');
        if(empty($contentA)){
            \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
            sleep(3);
            $contentA = \Common\Library\COM\CollectDo::getHtml($savePath,'','utf-8');
        }
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'<div id="content">','<script type="text/javascript" src="/js/chaptererror');

        $replace = array(
                '</div>','<a href="','<a href=','<a','</a>',
                '笔趣阁 www.biquge.tw','笔趣阁','www.biquge.tw',
                'www.biquge.tw','m.biquge.tw','biquge.tw');
        $contentT = str_replace($replace,'', $contentA);unset($contentA);

        if(!\Common\Library\COM\CollectDo::checkSpecialStr($contentT)){
            $data = array('id'=>$this->id,'dotime'=>time(),'disabled'=>4,'remark'=>'采集章节内容有误,包含特殊字符:'.$contentUrl);
            \HelloAdmin\Model\CrontabModel::updateData($data);
            $this->writeLog('采集章节内容有误,包含特殊字符:'.var_export($contentUrl,1).'@@'.var_export($contentT,1),'ERR');
        }
        $contentT = htmlentities($contentT,ENT_COMPAT,'UTF-8');
        $contentPath = \Common\Library\COM\CollectDo::doContentPath();
        $handel = fopen('.'.$contentPath,'x+');
        if($handel && fwrite($handel,$contentT) !== FALSE){
            fclose($handel);
            return $contentPath;
        }
        $this->writeLog('章节内容:'.var_export($contentT,1));
        $this->writeLog('章节内容存储失败!:'.var_export($contentPath,1).'|@|'.var_export($contentUrl,1),'ERR');
    }

    //采集日志记录
    private function writeLog($message,$level='INFO'){
        \Think\Log::write($message,$level,$type='','./Runtime/Collect/7kshu/logs-'.  date('Ymd').'.log');
        if($level == 'ERR'){
            exit($message);
        }
    }

    public function update(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/7kshu/updatelist.html';
        \Common\Library\COM\CollectDo::downPage($this->url, $pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        $updatelist = \Common\Library\COM\CollectDo::getSubstr($getStr,'最近更新小说列表</h2>','最新入库小说</h2>');
        unset($getStr,$pathFile);
        //获取书籍名称
        $count = mb_substr_count($updatelist,'<li>');
        //unset($articles,$updatelist);
        $Book = new \Common\Model\BookModel();
        $Author = new \Common\Model\BookAuthorModel();
        for($i=0;$i<$count;$i++){
            $now = \Common\Library\COM\CollectDo::getSubstr($updatelist,'','</li>');
            $bookName = \Common\Library\COM\CollectDo::getSubstr($now,'target="_blank">','</a></span><span');
            $authorName = \Common\Library\COM\CollectDo::getSubstr($now,'class="s4">','class="s5">');
            $authorName = rtrim($authorName,'</span><span"');
            //p($updatelist);p($now);p($authorName);p($bookName);
            if(empty($bookName) || empty($authorName)){
                $this->writeLog('书名,作者,或为空!:'.var_export($now,1).'@@'.var_export($updatelist,1),'ERR');
            }
            //p($bookName);p($authorName);
            $author_id = $Author->getAuthorId($authorName);
            $unique = md5($bookName.$author_id);
            //书本重复检测
            $this->book_id = $Book->checkBookRepeart($unique);
            if($this->book_id > 0){//更新已有的书籍
                $articleName = \Common\Library\COM\CollectDo::getSubstr($now,'.html"','class="s4');
                $articleName = \Common\Library\COM\CollectDo::getSubstr($articleName,'target="_blank">','</a></span><span');

                if(empty($articleName)){
                    $this->writeLog($offset.':====> 章节名称空'.var_export($now,1),'ERR');
                }
                $unique = md5($this->book_id.$articleName);
                $this->BookArticle = new \Common\Model\BookArticleModel();
                $result = $this->BookArticle->checkArtcileRepeat($unique);

                if($result){//重复章节检测
                    $this->writeLog($articleName.' ====> 重复章节');
                    continue;
                }else{
                    //获取章节数
                    $num = \Common\Library\COM\CollectDo::getArticleNum($articleName);
                    $piece_num = \Common\Library\COM\CollectDo::getPieceNum($articleName);
                    //获取章节内容地址
                    $articleUrl = \Common\Library\COM\CollectDo::getSubstr($now,'s3"><a href="','target="_blank">'.$articleName);
                    $articleUrl = rtrim($articleUrl,'"');
                    $articleUrl = $this->url.$articleUrl;
                    //p($value);p($articleUrl);exit;
                    //获取章节内容并存储
                    $contentPath = $this->contentCollection($articleUrl);
                    $new = $this->BookArticle->getNewArticle($this->book_id);

                    if(empty($num) || empty($new['article_num']) || $new['article_num']==($num-1)){//默认更新一个章节
                        $articleDate = array(
                            'book_id'=> $this->book_id,
                            'article_name'=> $articleName,
                            'create_time'=>  time(),
                            'year'=>  date('Y'),
                            'month'=>  date('m'),
                            'day'=>  date('d'),
                            'article_num'=> $num,
                            'update_time' => time(),
                            'unique'=>$unique,
                            'piece_num'=> $piece_num,
                            'piece_name'=> '',
                            'content'=>$contentPath
                        );
                        //p($articleDate);//exit;
                        $article_id = $this->BookArticle->add($articleDate);
                        $this->writeLog('====> 更新了 一个 章节 ======>');
                        continue;
                    }else{//更新多个章节
                        $this->detailUrl = \Common\Library\COM\CollectDo::getSubstr($now,'class="s2"><a href="',' target="_blank">'.$bookName);
                        $this->detailUrl = rtrim($this->detailUrl,'"');
                        $pathFile = './Runtime/Collect/7kshu/updateaddbook.html';
                        \Common\Library\COM\CollectDo::downPage($this->detailUrl, $pathFile);
                        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
                        $this->listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<dl>','</dl>');
                        //章节名称和篇名入库
                        $this->articleCollection();
                        $this->writeLog('====>  对比更新 多个 章节内容 =====>');
                    }
                }
            }
            $updatelist = \Common\Library\COM\CollectDo::getSubstr($updatelist,'</li>','',FALSE);
            $this->writeLog($bookName.'||====> 新书跳过，更新下一个章节 ====>');
            continue;
        }
    }

}
